package ooo.reindeer.commons.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.time.format.SignStyle;
import java.time.temporal.ChronoField;

import static java.time.temporal.ChronoField.*;

/**
 * @ClassName DateTimeUtil
 * @Author songbailin
 * @Date 2021/3/12 13:49
 * @Version 1.0
 * @Description TODO
 */
public class DateTimeUtil {

   public final static DateTimeFormatter LOCAL_DATE;
   public final static DateTimeFormatter LOCAL_TIME;
   public final static DateTimeFormatter LOCAL_DATETIME;
    static {
        LOCAL_DATE = new DateTimeFormatterBuilder()
                .appendValue(YEAR, 4)
                .optionalStart()
                .appendLiteral('-')
                .optionalEnd()
                .appendValue(MONTH_OF_YEAR, 2)
                .optionalStart()
                .appendLiteral('-')
                .optionalEnd()
                .appendValue(DAY_OF_MONTH, 2).toFormatter();

        LOCAL_TIME = (new DateTimeFormatterBuilder()).appendValue(ChronoField.HOUR_OF_DAY, 2)
                .optionalStart().appendLiteral(':').optionalEnd().appendValue(ChronoField.MINUTE_OF_HOUR, 2)
                .optionalStart()
                .appendLiteral(':').optionalEnd().optionalStart().appendValue(ChronoField.SECOND_OF_MINUTE, 2)
                .optionalEnd().optionalStart().appendLiteral('.').optionalEnd()
                .optionalStart().appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, false)
                .optionalEnd()
                .toFormatter();


        LOCAL_DATETIME=new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .append(LOCAL_DATE)
                .optionalStart()
                .appendLiteral('T')
                .optionalEnd()
                .optionalStart()
                .appendLiteral(' ')
                .optionalEnd()
                .append(LOCAL_TIME)
                .toFormatter();

    }

    public static void main(String[] args) {
        System.out.println(LocalDateTime.parse("2021-01-09T13:32:33.333333",LOCAL_DATETIME));
        System.out.println(LocalDateTime.parse("2021-01-09T13:32:33.333333333",LOCAL_DATETIME));
        System.out.println(LocalDateTime.parse("2021-01-09T13:32:33.333",LOCAL_DATETIME));
        System.out.println(LocalDateTime.parse("2021-01-09T13:32:33.33",LOCAL_DATETIME));
        System.out.println(LocalDateTime.parse("2021-01-09 13:32:33",LOCAL_DATETIME));
        System.out.println(LocalDateTime.parse("2021-01-09 13:32",LOCAL_DATETIME));
        System.out.println(LocalDateTime.parse("20210109133233999",LOCAL_DATETIME));
        System.out.println(LocalDateTime.parse("20210109 13:32:33.222",LOCAL_DATETIME));
        System.out.println(LocalDateTime.parse("202107081335",LOCAL_DATETIME));

    }
}
